package com.lanou.wuyou.gift.list;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.google.gson.Gson;
import com.lanou.wuyou.gift.R;
import com.lanou.wuyou.gift.base.BaseFragment;
import com.lanou.wuyou.gift.bean.ListBean;
import com.lanou.wuyou.gift.tools.NetResultInterface;
import com.lanou.wuyou.gift.tools.NetWorkInstance;
import com.lanou.wuyou.gift.tools.OnItemClickListener;

/**
 * Created by dllo on 17/3/16.
 */

public class ListListFragment extends BaseFragment implements OnItemClickListener{
    private RecyclerView recyclerView;
    private ListBean listBean;

    public static ListListFragment newInstance(String url){
        Bundle args = new Bundle();
        args.putString("url",url);
        ListListFragment listListFragment = new ListListFragment();
        listListFragment.setArguments(args);
        return listListFragment;
    }
    @Override
    public int setLayout() {
        return R.layout.item_list_recycler;
    }

    @Override
    public void initView(View view) {
        recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
    }

    @Override
    public void initData() {

        final ListAdapter adapter = new ListAdapter(context);
        adapter.setClickListener(this);
        final GridLayoutManager gridLayoutManager = new GridLayoutManager(context,2, LinearLayoutManager.VERTICAL,false);
        recyclerView.setLayoutManager(gridLayoutManager);
        recyclerView.setAdapter(adapter);
        gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                if (position == 0){
                    return gridLayoutManager.getSpanCount();
                }
                return 1;
            }
        });

        String urls = getArguments().getString("url");
        NetWorkInstance.getInstance().netRequest(urls, new NetResultInterface() {
            @Override
            public void success(String str) {
                Gson gson = new Gson();
                listBean = gson.fromJson(str,ListBean.class);
                adapter.setListBean(listBean);
            }

            @Override
            public void fail(String str) {

            }
        });
    }

    @Override
    public void CLick(View view, int position) {
        if (position != 0){
        int id = listBean.getData().getItems().get(position - 1).getId();
        Intent intent = new Intent(getActivity(),ListItemWebView.class);
        intent.putExtra("id",id);
        startActivity(intent);
        }
    }

    @Override
    public void onClick(View v) {

    }
}
